<template>
	<view class="rootView">
		<scroll-view scroll-y="true" class="mt-scroll">
			<view class="cp-body">
				<view class="cp-list" v-for="(item,index) in couponList" :key="index">
					<view class="flex-between" v-if="item.status != 3">
						<view class="left flex-column cff4">
							<text class="fz36 bold">¥ {{item.voucher_reduce}}</text>
							<text style="font-weight: 700; margin-top: 20rpx;" class="fz32">企服代金券</text>
						</view>
						<view class="middle">
							<view class="fz36" style="font-weight: 700;">{{item.voucher_name}}</view>
							<view class="time c-666" :class="{'cff4':item.isToday}">
								{{$moment(item.endtime_text).format('YYYY-MM-DD')}} 到期
							</view>
							<view class="rules flex c-999" @click="showRule(item)">
								使用规则<u-icon class="arrow" name="arrow-down" size="12"></u-icon>
							</view>
						</view>
						<image class="right-img" v-if="currentIndex == index" src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/coupon/icon_choose.png"
							mode="widthFix" @click="currentIndex = -1">
						</image>
						<image class="right-img" v-if="currentIndex != index" src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/coupon/icon_no.png"
							mode="widthFix" @click="currentIndex = index">
						</image>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="choose-btn">
			<view class="btn" @click="goConfirm">
				确定
			</view>
		</view>
		<u-popup class="showRules" :show="showRules" :round="15" :closeOnClickOverlay="true" mode="center"
			:customStyle="{width: '280px', minHeight: '500rpx',padding: '10px',textAlign:'center'}"
			@close="showRules = false">
			<view style="margin-bottom: 20px;" class="fz40">
				使用规则
			</view>
			<view class="rule-text">
				<rich-text :nodes="rule"></rich-text>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import personalApi from "@/api/personal.js"
	export default {
		data() {
			return {
				showRules: false, //规则弹窗
				rule: '', //规则
				couponList: [],
				currentIndex: -1, //当前选中的下标,
				amount: ''
			}
		},
		onLoad(e) {
			console.log(e)
			this.amount = e.amount
			this.getCouponList(e.couponId)
		},
		methods: {
			//确定选择
			goConfirm() {
				if (this.currentIndex > -1) { //选择了，则返回优惠券对象
					uni.$emit('returnData', this.couponList[this.currentIndex])
				} else { //没有选择则返回null
					uni.$emit('returnData', null)
				}
				uni.navigateBack({
					delta: 1
				})
			},

			//获取优惠券列表
			async getCouponList(couponId) {
				let res = await personalApi.getCouponList({status: 1, amount: this.amount})
				if (res.code == 1) {
					this.couponList = res.data
					if (couponId) {
						for (var i = 0; i < this.couponList.length; i++) {
							if (this.couponList[i].id == couponId) {
								this.currentIndex = i
								break
							}
						}
					}
				} else {
					this.$used.msg(res.msg)
				}
			},
			
			showRule(item) {
				this.showRules = true
				this.rule = item.voucher_rule
			}
		}
	}
</script>

<style lang="scss">
	@import url(@/common/css/reset.css);

	.rootView {
		position: relative;
		background: #F6F6F6;
		height: 100%;
		overflow-y: auto;
		
		.mt-scroll {
			height: calc(100vh - 153rpx);
		}
		.cp-body {
			padding: 0 30rpx;
			.cp-list {
				padding: 40rpx;
				margin-top: 20rpx;
				width: 690rpx;
				height: 213rpx;
				background: #fff;
				border-radius: 22rpx;

				.right-use {
					width: 152rpx;
					height: 58rpx;
					background: #FF6161;
					border-radius: 55px;
					color: #F4F8FB;
				}

				.right-img {
					margin-top: 40rpx;
					width: 47rpx;
					height: 47rpx;
				}

				.left {
					align-items: center;
				}


				.middle {
					.time {
						margin: 10rpx 0;
					}

					.arrow {
						margin-left: 20rpx;
					}
				}

			}
		}


	}

	.choose-btn {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 0 30rpx;
		display: flex;
		justify-content: center;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 85rpx;
			background: #FF6161;
			border-radius: 147rpx;
			color: #fff;
			font-size: 30rpx;
		}
	}
	
	.rule-text>rich-text {
		color: #666;
	}

	/deep/ .u-icon--right {
		margin-top: 10rpx;
		margin-left: 10rpx;
	}
</style>